#include "mypushbutton.h"
#include <QDebug>
#include "QPropertyAnimation"//动态跳动对象
//myPushButton::myPushButton(QWidget *parent) : QPushButton(parent)
//{

//}
myPushButton::myPushButton(QString normalImg, QString pressImg ){
    this->pressImgPath=pressImg;
    this->normalImgPath=normalImg;
    QPixmap pix;
    bool ret=pix.load(normalImg);
    if(!ret){
        return;
    }
    //设置按钮固定大小
    this->setFixedSize( pix.width(),pix.height());

    //设置不规则图片样式，这里选择无边框
    this->setStyleSheet("QPushButton{border:0px;}");

    //设置图标
    this->setIcon(pix);

    //设置图标大小
    this->setIconSize(QSize(pix.width(),pix.height()));

}

void myPushButton::zoom1(void){
        //创建动态对象
        QPropertyAnimation * animation = new QPropertyAnimation(this,"geometry");
        //设置动画时间间隔
        animation->setDuration(200);

        //起始位置
        animation->setStartValue(QRect(this->x(),this->y(),this->width(),this->height()));
        //结束位置
        animation->setEndValue(QRect(this->x(),this->y()+10,this->width(),this->height()));

        //设置弹跳曲线
        animation->setEasingCurve(QEasingCurve::OutBounce);

        //执行动画
        animation->start();
}
void myPushButton::zoom2(void){
    //创建动态对象
    QPropertyAnimation * animation = new QPropertyAnimation(this,"geometry");
    //设置动画时间间隔
    animation->setDuration(200);

    //起始位置
    animation->setStartValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
    //结束位置
    animation->setEndValue(QRect(this->x(),this->y(),this->width(),this->height()));

    //设置弹跳曲线
    animation->setEasingCurve(QEasingCurve::OutBounce);

    //执行动画
    animation->start();
}
void  myPushButton::mousePressEvent(QMouseEvent *ev){
    if(pressImgPath!=""){
        QPixmap pix;
        bool ret=pix.load(pressImgPath);
        if(!ret){
            return;
        }
        //设置按钮固定大小
        this->setFixedSize( pix.width(),pix.height());

        //设置不规则图片样式，这里选择无边框
        this->setStyleSheet("QPushButton{border:0px;}");

        //设置图标
        this->setIcon(pix);

        //设置图标大小
        this->setIconSize(QSize(pix.width(),pix.height()));
    }
    return QPushButton::mousePressEvent(ev);
}
void  myPushButton::mouseReleaseEvent(QMouseEvent *ev){
    if(pressImgPath!=""){
        QPixmap pix;
        bool ret=pix.load(normalImgPath);
        if(!ret){
            return;
        }
        //设置按钮固定大小
        this->setFixedSize( pix.width(),pix.height());

        //设置不规则图片样式，这里选择无边框
        this->setStyleSheet("QPushButton{border:0px;}");

        //设置图标
        this->setIcon(pix);

        //设置图标大小
        this->setIconSize(QSize(pix.width(),pix.height()));
    }
    return QPushButton::mouseReleaseEvent(ev);
}


